package com.example.jchuah.tiles;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.DragEvent;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import java.util.Random;


public class MyActivity extends Activity implements View.OnTouchListener, View.OnDragListener, FlingDetector.FlingListener {

    int numRows = 4;
    int numCols = 4;

    TileGridLayout grid;
    Random randGen = new Random();
    FlingDetector flingDetector = new FlingDetector(this);
    GestureDetector gestureDetector;





    public void onFlingLeft(View view){
       RowColPair coordinate;
        coordinate = grid.getRowColumn(view);
        RowColPair leftOf = coordinate.getLeft();


        if (grid.isValidRowColPair(leftOf)) {
            View justRemoved = grid.removeTile(leftOf);
            grid.removeTile(coordinate);
            grid.addTile(leftOf, view);
            grid.addTile(coordinate, justRemoved);
            }
        }
    public void onFlingRight(View view) {
        RowColPair coordinate;
        coordinate = grid.getRowColumn(view);
        RowColPair rightOf = coordinate.getRight();

        if (grid.isValidRowColPair(rightOf)) {
            View justRemoved = grid.removeTile(rightOf);
            //removes tile to the right

            if (justRemoved.getTag().equals(view.getTag())) {
                //checks for equality

                if (justRemoved.getTag().equals(R.layout.tile)) {
                    //remove tile that was flung
                    grid.removeTile(coordinate);
                    View newTile = generateTile(R.layout.type2);
                    grid.addTile(rightOf, newTile);
                    //if the tile just removed id= tile, adds type2
                    //generate a tile of type2, and add the generated tile to the grid at rightOf
                }
                if (justRemoved.getTag().equals(R.layout.type2)) ;
                {
                    grid.removeTile(coordinate);
                    View new2Tile = generateTile(R.layout.type3);
                    grid.addTile(rightOf, new2Tile);
                }

                if (justRemoved.getTag().equals(R.layout.type3)) ;
                grid.removeTile(coordinate);
                View new3Tile = generateTile(R.layout.type4);
                grid.addTile(rightOf, new3Tile);
            }
            grid.addTile(rightOf, view);
            grid.addTile(coordinate, justRemoved);
        }
    }

    public void onFlingUp(View view) {
        RowColPair coordinate;
        coordinate = grid.getRowColumn(view);
        RowColPair upOf = coordinate.getAbove();

        if (grid.isValidRowColPair(upOf)) {
            View justRemoved = grid.removeTile(upOf);
            grid.removeTile(coordinate);
            grid.addTile(upOf, view);
            grid.addTile(coordinate, justRemoved);
        }

    }

    public void onFlingDown(View view) {
        RowColPair coordinate;
        coordinate = grid.getRowColumn(view);
        RowColPair downOf = coordinate.getBelow();

        if (grid.isValidRowColPair(downOf)) {
            View justRemoved = grid.removeTile(downOf);
            grid.removeTile(coordinate);
            grid.addTile(downOf, view);
            grid.addTile(coordinate, justRemoved);
        }

    }

    public void generateGrid() {
        for (int row = 0; row < numRows; row++){
            for (int col = 0; col < numCols; col++) {
                int id = 0;
                switch (randGen.nextInt(2)) {
                    case 0:
                        id = R.layout.tile;
                        break;
                    case 1:
                        id = R.layout.type2;
                        break;
                }
                grid.addTile(row, col, generateTile(id));
            }
        }
    }
    public View generateTile(int id) {
        View tile = getLayoutInflater()
                .inflate(id, null);
        tile.setTag(id);

        View type2 = getLayoutInflater()m
                .inflate(id,null);
        type2.setTag(id);

        View type3 = getLayoutInflater()
                .inflate(id,null);
        type3.setTag(id);

        View type4 = getLayoutInflater()
                .inflate(id,null);
        type4.setTag(id);

        tile.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                flingDetector.touched = view;
                return gestureDetector.onTouchEvent(motionEvent);
            }
        });
        return tile;

    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        grid = (TileGridLayout)findViewById(R.id.gridView);
        generateGrid();
        gestureDetector = new GestureDetector(this, flingDetector);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.my, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onDrag(View view, DragEvent dragEvent) {
        return false;
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        return false;
    }

}

